package cn.wolfcode.luowowo.acticle.service.impl;


import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.domain.Region;
import cn.wolfcode.luowowo.acticle.mapper.DestinationMapper;
import cn.wolfcode.luowowo.acticle.mapper.RegionMapper;
import cn.wolfcode.luowowo.acticle.query.DestinationQuery;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationMapper destinationMapper;

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public PageInfo query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        return new PageInfo(destinationMapper.selectForList(qo));
    }

    @Override
    public PageInfo queryDest(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        return new PageInfo(destinationMapper.selectDestById(qo));
    }

    @Override
    public void saveOrUpdate(Destination destination) {
        if(destination.getId() == null){
            destinationMapper.insert(destination);
        }else{
            destinationMapper.updateByPrimaryKey(destination);
        }
    }

    @Override
    public void delete(Long id) {
        destinationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Destination> getToastById(Long parentId) {
        List<Destination> list = new ArrayList<>();
        createToast(list, parentId);
        Collections.reverse(list);
        return list;
    }

    @Override
    public void changeHotValue(Long id, boolean hot) {
        destinationMapper.updateHotValue(id, hot);
    }

    @Override
    public void setInfo(Long id, String info) {
        destinationMapper.updateInfo(id, info);
    }

    private void createToast(List<Destination> list, Long parentId){

        Destination destination = destinationMapper.selectByPrimaryKey(parentId);

        if(destination == null){
            return;
        }

        list.add(destination);
        if(destination.getParent() == null){
            return;
        }
        createToast(list, destination.getParent().getId());
    }


    @Override
    public boolean isAbroad(Long id) {

        Destination destination = destinationMapper.selectByPrimaryKey(id);

        if(destination == null){
            return false;
        }

        if("中国".equals(destination.getName())){
            return true;
        }


        if(destination.getParent() != null){
            if("中国".equals(destination.getParent().getName())){
                return true;
            }else{
                isAbroad(destination.getParent().getId());
            }
        }
        return false;
    }

    @Override
    public List<Map<String, Object>> getDestByDeep(int deep) {
        return destinationMapper.selectDestinationByDeep(deep);
    }

    @Override
    public Destination get(Long id) {
        return destinationMapper.selectByPrimaryKey(id);
    }

    @Override
    public Destination getCountry(Long id) {
       return getCountryP(id);
    }
    private Destination getCountryP(Long id){
        Destination destination = destinationMapper.selectByPrimaryKey(id);
        if(destination.getParent() == null){
            return destination;
        }
        return getCountryP(destination.getParent().getId());
    }

    @Override
    public Destination getProvince(Long id) {
        return getProvinceP(id);

    }
    private Destination getProvinceP(Long id){
        Destination destination = destinationMapper.selectByPrimaryKey(id);

        if(destination.getDeep() < 2){
            return null;
        }
        //省份
        if(destination.getDeep() == 2){
            return destination;
        }
        //城市
        if(destination.getDeep() == 3){
            return destination.getParent();
        }
        if(destination.getDeep() > 3){
            getProvinceP(destination.getParent().getId());
        }
        return null;
    }


    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    @Override
    public List<Destination> getDestByRegionId(Long rid) {
        Region region = regionMapper.selectByPrimaryKey(rid);
        return destinationMapper.selectDestinationByIds(region.getRefIds());
    }

    @Override
    public List<Destination> getDestByStrDestIds(String[] arrDestIds) {
        return destinationMapper.selectDestByStrDestIds(arrDestIds);
    }


}
